package com.ruoyi.system.utils;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

public class CloakUtils {

    private static final Logger logger = LoggerFactory.getLogger(CloakUtils.class);

    private static final String API_URL = "www.tiktokba.com/cloak/byApi";
    private static final String ESCLOAK_KEY = "PlJm4UAYsHRcMXBExWdSTza9fkFgojqQ";

    public static String requestForHttp(String params) {
        /** 创建HttpClient */
        CloseableHttpClient httpClient = HttpClients.createDefault();
        RequestConfig defaultRequestConfig = RequestConfig.custom()
                .setSocketTimeout(5000)
                .setConnectTimeout(5000)
                .setConnectionRequestTimeout(5000)
                .setStaleConnectionCheckEnabled(true)
                .build();
        /** httpPost */
        HttpPost httpPost = new HttpPost(API_URL);
        httpPost.setConfig(defaultRequestConfig);
        httpPost.setHeader("Content-Type","application/json");
        httpPost.setHeader("escloak-key",ESCLOAK_KEY);
        List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
//        Iterator<Map.Entry<String,Object>> it = params.entrySet().iterator();
//        while(it.hasNext()){
//            Map.Entry<String,Object> en = it.next();
//            String key = en.getKey();
//            String value = en.getValue().toString();
//            paramsList.add(new BasicNameValuePair(key,value));
//        }
        CloseableHttpResponse httpResponse = null;
        try{
            httpPost.setEntity(new StringEntity(params.substring(0, params.length() - 1), StandardCharsets.UTF_8));
            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity,"UTF-8");
            JSONObject json = JSONObject.parseObject(result);
            logger.info("搜索结果:{}",json);
            if (json.get("errorCode").equals("0")) {
                return JSONObject.parseObject(result).get("data").toString();
            }
            EntityUtils.consume(httpEntity);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try{
                if(httpResponse!=null){
                    httpResponse.close();
                }
            }catch(IOException e){
                e.printStackTrace();
            }
        }
        return null;
    }
}
